<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%@ include file="/common/operatehistory.jsp"%>
<%@ include file="/common/message.jsp"%>
<%@ include file="/common/user.jsp"%>
<%!
	public void recordInfo(OperateHistory oh) {
		Integer category = oh.getCategory();
		Integer operate = oh.getOperate();
		Integer uid = Tools.getInt(oh.getUid());
		String oid = oh.getOid();
		User commentUser = userByUid(uid);
		// 暂时只提醒点赞信息(对问题，评论，步数点赞的提醒)
		if (!operate.equals(LOVE))
			return;
		if (category.equals(QUEST_CATEGORY)) {
			List<Quest> questList = QBuilder.db("basedb").select().from("Quest").where(new Where("status = ", STATUS_ENABLE).and("id=", oid)).query().getResultObj();
			if (!isEmpty(questList)) {
				Quest quest = questList.get(0);
				String title = quest.getTitle();
				Integer questUid = quest.getUid();
				if (questUid.equals(uid))
					return;
				if (title.equals("发布圈子") || title.equals("药师服务")) {
					title = quest.getContent();
				}
				if (title.length() > 10) {
					title = title.substring(0, 10) + "...";
				}
				String tips = String.format("%s参与并赞了帖子“%s”，请您查看", new Object[]{getDisplayName(commentUser), title});
				if (quest.getTitle().equals("药师服务"))
					createMsg(questUid, oh.getHospital(), "药师服务点赞提醒", "药师服务提醒", tips, QUESTINFO, oid);
				else
					createMsg(questUid, oh.getHospital(), "圈子点赞提醒", "圈子提醒", tips, QUESTINFO, oid );
			}
		} else if (category.equals(COMMENT_CATEGORY)) {
			List<com.tianxiabuyi.model.pojo.Comment> commentList = QBuilder.db("basedb").select().from("Comment").where(new Where("status = ", STATUS_ENABLE).and("id=", oid)).query().getResultObj();
			if (!isEmpty(commentList)) {
				com.tianxiabuyi.model.pojo.Comment comment = commentList.get(0);
				String content = comment.getContent();
				Integer commentUid = comment.getUid();
				if (commentUid.equals(uid))
					return;
				if (content.length() > 10) {
					content = content.substring(0, 10) + "...";
				}
				String tips = String.format("%s赞了你的评论“%s”，请您查看", new Object[]{getDisplayName(commentUser), content});
				List<Quest> questList = QBuilder.db("basedb").select().from("Quest").where(new Where("status = ", STATUS_ENABLE).and("id=", comment.getSrcId())).query().getResultObj();
				if (!isEmpty(questList)) {
					Quest quest = questList.get(0);
					// 点赞后是否需要向该问题有关人员全部推送，待确定，现向直接人员推送
					if (quest.getTitle().equals("药师服务"))
						createMsg(commentUid, oh.getHospital(), "药师服务点赞提醒", "药师服务提醒", tips, QUESTINFO, comment.getSrcId() + "");
					else
						createMsg(commentUid, oh.getHospital(), "圈子点赞提醒", "圈子提醒", tips, QUESTINFO, comment.getSrcId() + "");
				}
			}
		} else if (category.equals(STEP_CATEGORY)) {
			List<Map> steps = QBuilder.db("basedb").select().from("steps").where(new Where("id=", oid)).query().getResultObj();
			if (!isEmpty(steps)) {
				Map stepMap = steps.get(0);
				Integer stepUid = (Integer)stepMap.get("uid");
				if (stepUid.equals(uid))
					return;
				String content = Tools.getDesc(stepMap.get("step_time"));
				String tips = String.format("%s赞了你“%s”的步数", new Object[]{getDisplayName(commentUser), content});
				createMsg(stepUid, oh.getHospital(), "步数点赞提醒", "系统提醒", tips, SYSTEMINFO, oid);
			}
		} else {

		}
	}
%>
<%
	OperateHistory oh = OperateHistory.fromMap(parameters, OperateHistory.class);
	Integer flag = oh.getOperate();
	if (!SEE.equals(flag)) {
		msg = Token.validateToken(input);
		errcode = msg.getErrcode();
		if (0 != errcode) {
			Tools.encryptPrint(out, msg);
			return;
		}
		Token token = (Token)msg.get("token");
		msg = new Message();
		oh.setUid(token.getUid() + "");
	}
	Tools.encryptPrint(out, operate(oh, false));
	recordInfo(oh);
%>